package com.frxuexi.park2209.fragment

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.JavascriptInterface
import android.widget.Toast
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.base.net.Const
import com.frxuexi.demo.diff.DiffActivity
import com.frxuexi.demo.news.NewsActivity
import com.frxuexi.park2209.WebActivity
import com.frxuexi.park2209.bus.LiveDataBus
import com.frxuexi.park2209.bus.TextMessage
import com.frxuexi.park2209.databinding.FragmentHomeBinding
import com.frxuexi.park2209.mynews.MyNewsActivity
import com.frxuexi.park2209.notice.NoticeActivity
import com.frxuexi.park2209.park.ParkActivity
import com.frxuexi.park2209.patrol.PatrolActivity
import com.frxuexi.park2209.repair.AddRepairActivity
import com.frxuexi.park2209.repair.RepairActivity
import com.frxuexi.park2209.sign.SignActivity
import com.frxuexi.park2209.sign.SignListActivity
import com.frxuexi.park2209.user.LoginActivity
import com.frxuexi.park2209.visitor.VisitorActivity


class HomeFragment : Fragment() {

    lateinit var binding: FragmentHomeBinding
    lateinit var myurl:String
    lateinit var listener:WebViewFun
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding=FragmentHomeBinding.inflate(layoutInflater, container, false)
        myurl=SPUtils.getInstance().getString("home","")
        binding.homeHeader.headerTv.apply {
            visibility=View.VISIBLE
            setOnClickListener {
                SPUtils.getInstance().remove(Const.TOKEN)
                SPUtils.getInstance().remove(Const.UNAME)
                SPUtils.getInstance().remove(Const.UID)
                startActivity(Intent(requireContext(),LoginActivity::class.java))
            }
        }
        LiveDataBus.with("msg").observe(requireActivity(),object :Observer<TextMessage>{
            override fun onChanged(value: TextMessage) {
                if(value!=null)
                {
                    ToastUtils.showLong(value.msg)
                }
            }

        })
        initWebView()

        return binding.root
    }

    private fun initWebView() {
        listener=WebViewFun()
        binding.webview.apply {
            loadUrl(myurl)
            settings.javaScriptEnabled=true
            addJavascriptInterface(listener,"androidinfo")
        }
    }

    inner class WebViewFun{

        @JavascriptInterface
        fun androidsign(id: String) {
            //打开签到activity
            Log.i("===","签到点击了")
            start(SignActivity::class.java)
        }
        @JavascriptInterface
        fun androidaddculture(id: String) {
            //打开新闻activity
        }
        @JavascriptInterface
        fun androidparking(id: String?) {
            //车位申请
            start(ParkActivity::class.java)
        }
        @JavascriptInterface
        fun androidapply(id: String?) {
            //我的申请
            start(DiffActivity::class.java)
        }
        @JavascriptInterface
        fun androidvisit(id: String?) {
            //来访预约
//            start(NewsActivity::class.java)
            start(VisitorActivity::class.java)
        }
        @JavascriptInterface
        fun androidpatrol(id: String?) {
            //巡更管理
            start(PatrolActivity::class.java)
        }
        @JavascriptInterface
        fun androidnews(id: String?) {
        }
        @JavascriptInterface
        fun androidnoticelist(id: String?) {
        }
        @JavascriptInterface
        fun androidrepair(id: String?) {
            //维修管理
            start(RepairActivity::class.java)
        }
        @JavascriptInterface
        fun androidaddrepair(id: String?) {
            //添加维修
            start(AddRepairActivity::class.java)
        }
        @JavascriptInterface
        fun androidaddnotice(id: String?) {
            //公告管理
//            start(NoticeActivity::class.java)
            ARouter.getInstance().build("/app/NoticeActivity").navigation()
        }
        @JavascriptInterface
        fun androidculture(id: String?) {
            //文化管理
//            start(MyNewsActivity::class.java)
            ARouter.getInstance().build("/demo/NewsActivity").navigation()
        }
        @JavascriptInterface
        fun androidnotice(id: String?) {
            //公告管理
            start(WebActivity::class.java)
        }
        @JavascriptInterface
        fun androidattendance(id: String?) {
            //考勤管理
            start(SignListActivity::class.java)
        }
        @JavascriptInterface
        fun androidproperty(id: String?) {
            //物业审核
        }
        @JavascriptInterface
        fun androidcheckculture(id: String?) {
            //文化审核
        }
        @JavascriptInterface
        fun androidpeople(id: String?) {
            //人资管理
        }
    }

    fun start(clazz: Class<*>){
        startActivity(Intent(requireContext(),clazz))
    }
}